package com.study.d012Components.media;

import java.net.URL;
import java.util.Map;

import javafx.animation.Animation;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.AudioClip;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaMarkerEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.Track;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.util.Pair;

/**视频播放器
*@description
*@author xtayhame
*date 2020年3月18日
*version
*since v1.0
*/
public class MediaPlayMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		HBox hBox = new HBox(20);

		Button playButton = new Button("播放");
		Button stopButton = new Button("暂停");
		Button resetButton = new Button("重新播放");
		Button muteButton = new Button("静音");
		Button infoButton =  new Button("获取信息");
		//记载 将军令  音乐
		URL url = FXMLLoader.getDefaultClassLoader().getResource("com/study/d012Components/audio/2.mp3");
		System.out.println(url.toExternalForm());

		Media media = new Media("file:/D:/2.mp3");
		MediaPlayer player = new MediaPlayer(media);
		//设置平衡  -1:左声道   1:右声道
		player.setBalance(0);
		player.setAutoPlay(true);

		//播放次数，  Animation.INDEFINITE 无限次
		player.setCycleCount(MediaPlayer.INDEFINITE);

		//甚至音量大小
		//player.setVolume(0.5);

		//设置播放速度    1是正常播放
		//player.setRate(0.1);

		//设置自动播放
		//player.setAutoPlay(true);

		//设置开始播放时间和结束播放时间
		//player.setStartTime(Duration.seconds(10));
		//player.setStopTime(Duration.seconds(20));

		hBox.getChildren().addAll(playButton,stopButton,resetButton,muteButton,infoButton);
		anchorPane.getChildren().add(hBox);
		stage.setScene(new Scene(anchorPane, 400, 300));
		stage.show();


		//添加标记信息
		media.getMarkers().put("a", Duration.seconds(5));
		media.getMarkers().put("b", Duration.seconds(10));
		media.getMarkers().put("c", Duration.seconds(20));


		playButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				player.play();
			}
		});
		stopButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				player.pause();
			}
		});

		resetButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//销毁音乐
				//player.dispose();
				//
				System.out.println("获取当前播放时间："+player.getCurrentTime().toSeconds());
				//这个值获取不了
				System.out.println("获取总的播放时常："+player.getTotalDuration().toSeconds());

				//重新播放应该是先暂停，然后设置时间
				player.pause();
				player.setStartTime(Duration.seconds(0));
				player.play();
			}
		});
		muteButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(player.isMute()){
					player.setMute(false);
				}else{
					player.setMute(true);
				}

			}
		});

		//
		player.setOnEndOfMedia(new Runnable() {
			@Override
			public void run() {
				System.out.println("播放完毕");
			}
		});

		player.setOnError(new Runnable() {
			@Override
			public void run() {
				System.out.println("播放出错");
			}
		});

		player.setOnHalted(new Runnable() {
			@Override
			public void run() {
				System.out.println("播放停止");
			}
		});

	/*	//设置声谱的时间间隔， 默认0.1秒
		player.setAudioSpectrumInterval(0.1);
		//设置每次取的频谱数， 默认128
		player.setAudioSpectrumNumBands(20);
		//设置频谱的起始点
		player.setAudioSpectrumThreshold(10);*/

		//监听音乐频谱
		player.setAudioSpectrumListener(new AudioSpectrumListener() {
			/**
			 * timestamp:音乐的当前时间
			 * duration ：频率
			 * magnitudes：频谱
			 * phases
			 */
			@Override
			public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {

			}
		});


		//获取标记信息
		player.setOnMarker(new EventHandler<MediaMarkerEvent>() {
			@Override
			public void handle(MediaMarkerEvent event) {
				//可以将歌词 以<String ,Duration> 形式存放在marker中， 然后在这里添加歌词显示逻辑

				Pair<String, Duration> pari=event.getMarker();
				System.out.println("MediaMarkerEvent   key:" + pari.getKey()  + "     value:"+pari.getValue().toSeconds());
			}
		});

		player.setOnPaused(new Runnable() {
			@Override
			public void run() {
				System.out.println("暂停播放");
			}
		});

		player.setOnReady(new Runnable() {
			@Override
			public void run() {
				System.out.println("准备播放");
			}
		});

		player.setOnStalled(new Runnable() {
			@Override
			public void run() {
				System.out.println("Stalled 播放出错");
			}
		});

		player.setOnStopped(new Runnable() {
			@Override
			public void run() {
				System.out.println("播放停止");
			}
		});


		player.statusProperty().addListener(new ChangeListener<MediaPlayer.Status>() {

			@Override
			public void changed(ObservableValue<? extends Status> observable, Status oldValue, Status newValue) {
				System.out.println("时时状态：" + newValue==null?"":newValue);
			}
		});


		infoButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				ObservableMap<String, Object>  data=media.getMetadata();
				System.out.println("获取原始信息：");
				for(String key:data.keySet()){
					System.out.println("		key:"+key + "   -   " + data.get(key));
				}

				System.out.println("获取track信息：");
				ObservableList<Track> track=media.getTracks();
				for(Track t:track){
					System.out.println( "          name:"+t.getName());
					System.out.println( "          trackID:"+t.getTrackID());
					System.out.println( "          Locale:"+t.getLocale());
					System.out.println( "          Metadata:"+t.getMetadata());
				}

				System.out.println("获取标记信息：");
				ObservableMap<String, Duration> marker=media.getMarkers();
				for(String key:marker.keySet()){
					System.out.println("      key:"+key + "      value:"+marker.get(key));
				}

			}
		});


		media.durationProperty().addListener(new ChangeListener<Duration>() {
			@Override
			public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
				if(newValue !=null){
					System.out.println("durationProperty()="+newValue   + "    " + newValue.toSeconds() + "秒");
				}

			}
		});
	}
}